from pyquery import PyQuery as pq


"""
  使用示例
"""
html = '''
<div id="container">
    <ul class="list">
         <li class="item-0">first item</li>
         <li class="item-1"><a href="link2.html">second item</a></li>
         <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
         <li class="item-1 active"><a href="link4.html">fourth item</a></li>
         <li class="item-0"><a href="link5.html">fifth item</a></li>
     </ul>
 </div>
'''
# doc = pq(html)
# print(doc('#container .list li')) #就是CSS选择器的写法 获取所有li标签
# print(type(doc('#container .list li'))) #<class 'pyquery.pyquery.PyQuery'>


"""
  查找节点示例01--找子孙节点
  find() 方法会将符合条件的所有子孙节点选择出来，结果的类型是 PyQuery 类型
"""

# doc = pq(html)
# items = doc('.list')
# print(type(items))
# print(items) # 整个ul
# lis = items.find('li') # ul下面所有li
# print(type(lis))
# print(lis)

"""
  查找节点示例02--找子节点
  children() 方法只会筛选儿子节点
"""
html2 = '''
<div id="container">
    <ul class="list">
         <li class="item-0">
            <ul><li class="item-2">first item</li></ul>
         </li>
         <li class="item-1"><a href="link2.html">second item</a></li>
         <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
         <li class="item-1 active"><a href="link4.html">fourth item</a></li>
         <li class="item-0"><a href="link5.html">fifth item</a></li>
     </ul>
 </div>
'''
doc = pq(html2)
# print(doc('.list').find('li'))
# print(doc('.list').children('li'))
# 遍历元素可以看到 doc('.list').children('li')  只获取到子li，class=item-2 的li是没有获取到的
for li in doc('.list').find('li').items():
    print(li.text())

print('******************************************')
for li in doc('.list').children('li').items():
    print(li.text())

# 获取active li
print(doc('.list').children('.active'))
